<?php

// @formatter:off
/**
 * @file t.items.betterone.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: t.items.betterone.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/tools/t.items.betterone.php $
 */
$wcToolScripts["item.betterone"] = array(
	"name" => "Better One",
	"description" => "Replaces a comic or image by a better version",
	"class" => "WCToolBetterOne",
	"url" => "http://code.google.com/p/wcomix/",
	"enabled" => true
);

// @formatter:on

class WCToolBetterOne extends WCTool {
	//
	// Public methods.
	public function run($spacer = "") {
		global $wcDefaults;
		global $wcOptions;
		//
		// Checking that Read Only Mode is not activated.
		if(!$wcDefaults["read-only"]) {
			if($wcOptions->activated("Comic") && $wcOptions->activated("Image")) {
				echo $spacer.WCColorText::ShellRed("Failed").": Both image and comic id can't be specified at a single time.\n";
			} elseif(($wcOptions->activated("Comic") || $wcOptions->activated("Image")) && $wcOptions->activated("Path")) {
				$ok = true;

				$path = $wcOptions->value("Path");
				//
				// Cleaning paths.
				while(strpos($path, '//')) {
					$path = str_replace('//', '/', $path);
				}
				if($path[strlen($path) - 1] == '/') {
					$path = substr($path, 0, strlen($path) - 1);
				}

				if(!is_file($path) || !is_readable($path)) {
					if(!is_file($path)) {
						echo $spacer.WCColorText::ShellRed("Failed").": Path '{$path}' is not a file.\n";
					} else {
						echo $spacer.WCColorText::ShellRed("Failed").": Path '{$path}' is not readable.\n";
					}
				} elseif($wcOptions->activated("Image")) {
					$this->betterImage($path, $spacer);
				} elseif($wcOptions->activated("Comic")) {
					$this->betterComic($path, $spacer);
				} else {
					echo $spacer.WCColorText::ShellRed("Failed").": Only chickens here.\n";
				}
			} elseif(!$wcOptions->activated("Comic")) {
				echo $spacer.WCColorText::ShellRed("Failed").": No comic ID specified.\n";
			} elseif(!$wcOptions->activated("Image")) {
				echo $spacer.WCColorText::ShellRed("Failed").": No image ID specified.\n";
			} elseif(!$wcOptions->activated("Path")) {
				echo $spacer.WCColorText::ShellRed("Failed").": No replacement file path specified.\n";
			} else {
				$this->showHelp($spacer);
			}
		} else {
			echo $spacer.WCColorText::ShellYellow("ReadOnlyMode activated.\n");
		}
	}
	public function showHelp($spacer = "") {
		echo "{$spacer}Options:\n";
		echo "{$spacer}\t-c comic-id, --comic comic-id\n";
		echo "{$spacer}\t\tID of a comic to be replaced.\n\n";
		echo "{$spacer}\t-i image-id, --image image-id\n";
		echo "{$spacer}\t\tID of an image to be replaced.\n\n";
		echo "{$spacer}\t-p file-path, --path file-path\n";
		echo "{$spacer}\t\tReplacement full path.\n\n";
	}
	//
	// Protected methods.
	protected function betterComic($path, $spacer) {
		//
		// Loading global pointers.
		global $wcOptions;

		$id = $wcOptions->value("Comic");
		$comic = WCComicsHolder::I()->item($id);
		if($comic && $comic->ok()) {
			echo "{$spacer}Replacing comic {$comic} by a better version: ";

			if(copy($path, $comic->path())) {
				@recursive_unlink($comic->previewDirPath());
				@unlink($comic->previewPath());
				$comic->shrink();
				$comic->generated = false;
				echo WCColorText::ShellGreen("Done");
			} else {
				echo WCColorText::ShellRed("Failed");
			}

			echo "\n";
		} else {
			echo $spacer.WCColorText::ShellRed("Failed").": There's something wrong with current comic";
		}
	}
	protected function betterImage($path, $spacer) {
		//
		// Loading global pointers.
		global $wcOptions;

		$id = $wcOptions->value("Image");
		$image = WCImagesHolder::I()->item($id);
		echo "{$spacer}Replacing image {$image} by a better version: ";
		if($image && $image->ok()) {
			if(copy($path, $image->path())) {
				$image->shrink();
				@unlink($image->previewPath());
				$image->setValidPreviews(false);
				echo WCColorText::ShellGreen("Done\n");
				echo "{$spacer}Regenerating preview: ";
				$image->generatePreviews();
				echo WCColorText::ShellGreen("Done\n");
			} else {
				echo WCColorText::ShellRed("Failed\n");
			}
		} else {
			echo $spacer.WCColorText::ShellRed("Failed").": There's something wrong with current image\n";
		}
	}
	protected function loadOptions() {
		if(!$this->_optionsLoaded) {
			global $wcOptions;

			if($wcOptions->addValueOption("Comic")) {
				$wcOptions->addOptionTrigger("Comic", "c");
				$wcOptions->addOptionTrigger("Comic", "comic");
			}
			if($wcOptions->addValueOption("Image")) {
				$wcOptions->addOptionTrigger("Image", "i");
				$wcOptions->addOptionTrigger("Image", "image");
			}
			if($wcOptions->addValueOption("Path")) {
				$wcOptions->addOptionTrigger("Path", "p");
				$wcOptions->addOptionTrigger("Path", "path");
			}

			$wcOptions->check();

			$this->_optionsLoaded = true;
		}
	}
}
